import 'package:myapp/http/sample/base/app_token_keeper.dart';
import 'package:uuid/uuid.dart';

import '../../core/base_request.dart';
import 'app_base_request.dart';
import 'app_encrypt.dart';

abstract class AppPostRequest<R> extends AppBaseRequest<R> {

  //jsonBody
  final Map<String, dynamic> _bodyDataParams = {};

  @override
  HttpMethod httpMethod() {
    return HttpMethod.POST;
  }

  bool needLogin(){
    return false;
  }

  String commandId();

  void addDataBody(String key,dynamic value){
    _bodyDataParams[key]=value;
  }


  @override
  void prepareRequest() {
    //header
    addHeader("content-type", "application/json;charset=UTF-8;");
    addHeader("Content-Type", "application/json");

    if(needLogin()){
      addHeader("accessToken", AppTokenKeeper.getInstance().getToken());
    }

    //body
    int timestamp = DateTime.now().millisecondsSinceEpoch;
    addRequestBody("time", timestamp);
    addRequestBody("commandId", commandId());
    addRequestBody("requestId", const Uuid().v4());
    var currentObj = EncryptUtils.sortMapByKey(_bodyDataParams);
    addRequestBody("data", currentObj);
    addRequestBody("key", EncryptUtils.encrypt(currentObj, timestamp));
  }

}